-- buff逻辑基类
local BuffBase = class("BuffBase")
local FightConst = require("const.FightConst")
local GameUtils = require("utils.GameUtils")
local AttributeConst = require("const.AttributeConst")

function BuffBase:ctor(entity, whoEntity, config)
    self._parent = nil
    self:setEntity(entity)
    self:setWhoEntity(whoEntity)
    self:setConfig(config)
end

function BuffBase:getParent()
    return self._parent
end

function BuffBase:setParent(parent)
    self._parent = parent
end

function BuffBase:setEntity(entity)
    self._entity = entity
end

function BuffBase:getEntity()
    return self._entity
end

function BuffBase:setWhoEntity(whoEntity)
    self._whoEntity = whoEntity
end

function BuffBase:getWhoEntity()
    return self._whoEntity
end

function BuffBase:getId()
    return self._config.id
end

function BuffBase:getType()
    return self._config.check_time
end

function BuffBase:getParams()
    return self._params
end

function BuffBase:setConfig(config)
    self._config = config
    self._frequency = self._config.chixu_count
end

function BuffBase:getConfig()
    return self._config
end

-- 激活buff
function BuffBase:awake()
    self:parseParams(self._config.params)
end

-- 检测buff
function BuffBase:check()
    self._frequency = self._frequency - 1
    if self._frequency == 0 then
        self:remove()
    end
end

function BuffBase:remove()
    self:onDestroy()
    if self._parent then
        self._parent:removeChild(self)
    end
end

function BuffBase:_trigger()
end

function BuffBase:parseParams(params)
    local paramSplit = string.split(params, "|")
    if #paramSplit ~= 3 then
        return
    end
    local attrId = tonumber(paramSplit[1])
    local value = paramSplit[3]
    local typeOpt = paramSplit[2]
    local per = string.find(value, "p")
    if per then
        local attrValue = string.gsub(value, "p", "")
        local targetValue = self._whoEntity:getAttr():getAttrValue(AttributeConst.ATTACK)
        local realValue = math.floor(targetValue * (attrValue / 100))
        self._params = {["typeOpt"] = typeOpt, ["attrId"] = attrId, ["value"] = realValue}
    else
        self._params = {["typeOpt"] = typeOpt, ["attrId"] = attrId, ["value"] = tonumber(value)}
    end
end

function BuffBase:onDestroy()
end

return BuffBase
